package com.zw.cc.stock.util;

import com.zw.cc.stock.common.Constants;
import com.zw.cc.stock.model.QuotesPo;
import com.zw.cc.stock.model.dto.KDJPolicyDto;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhaowuz on 2018/1/10
 */
public class calculateutil {

    public static List<Integer> calcCCICalculate(List<QuotesPo> list) {
        int TP, MAT, MA = 0, MDT, MD = 0;
        int P = 14;//14
        List<Integer> cci = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MAT = MDT = 0;
            cci.add(0);
            for (int j = 0; j < P; j++) {
                if (i - j < 0) {
                    break;
                }
                MAT += parseInt(list.get(i - j).getClose(),2);
                MA = MAT / (j + 1);
            }
            for (int j = 0; j < P; j++) {
                if (i - j < 0) {
                    break;
                }
                MDT += Math.abs(MA - parseInt(list.get(i - j).getClose(),2));
                MD = MDT / (j + 1);
            }
            TP = parseInt((list.get(i).getHighest() + list.get(i).getLowest() +list.get(i).getClose()),2) / 3;
            if (MD != 0) {
                cci.set(i, 200 * (TP - MA) / 3 / MD);
            }
        }
        return cci;
    }

    /**
     * float 四舍五入 乘以10 的倍数 转int
     * @param source 要转的值
     * @param digits 小数位
     * @return
     */
    public static int parseInt(float source, int digits) {
        if (source == Float.POSITIVE_INFINITY || source == Float.NEGATIVE_INFINITY || source == Float.NaN) {
            return 0;
        }
        source = BigDecimal.valueOf(source).setScale(digits, RoundingMode.HALF_UP).floatValue();
        return (int) (source * Math.pow(10, digits));
    }



    public static List<KDJPolicyDto> calKDJList(List<QuotesPo> list) {
        float RSV, K, D, J;
        float prevK = Constants.KDJ_DEFAULT_PREV_K; // 第一日的 K
        float prevD = Constants.KDJ_DEFAULT_PREV_D; // 第一日的 D
        List<KDJPolicyDto> kdjList = new LinkedList<KDJPolicyDto>();
        for (int i = 1, size = list.size(); i < size; i++) {
            RSV = calRSV(i, list);
            K = prevK * (Constants.KDJ_K_TERM_P2 - 1) / Constants.KDJ_K_TERM_P2 + RSV / Constants.KDJ_K_TERM_P2;
            D = prevD * (Constants.KDJ_D_TERM_P3 - 1) / Constants.KDJ_D_TERM_P3 + K / Constants.KDJ_D_TERM_P3;
            J = 3 * K - 2 * D;
            kdjList.add(new KDJPolicyDto(K, D, J));
            prevK = K; // 第一日的 K
            prevD = D; // 第一日的 D
        }
        return kdjList;
    }

    public static float calRSV(int index, List<QuotesPo> list) {
        int KDJ_TERM_P1 = Constants.KDJ_TERM_P1;// KDJ周期为9
        float AX, BX, NL = 0f, NH = 0f;
        QuotesPo quot = null;
        for (int j = 0; j < KDJ_TERM_P1; j++) {
            if (j == 0) {
                quot = list.get(index - j);
                NL = quot.getLowest();
                NH = quot.getHighest();
            }
            if (index - j >= 0) {
                quot = list.get(index - j);
                if (NL > quot.getLowest()) {
                    NL = quot.getLowest();
                }
                if (NH < quot.getHighest()) {
                    NH = quot.getHighest();
                }
            }
        }
        AX = list.get(index).getClose() - NL;
        BX = NH - NL;
        if (BX == 0) {
            BX = 1;
        }
        return AX * 100 / BX;
    }



}
